import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class Leetcode977 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(sortedSquares(new int[]{-7,-3,2,3,11})));
    }
    public static int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int[] res = new int[nums.length];
        int currentIndex = nums.length - 1;

        while (left <= right) {
            double powLeft = Math.pow(nums[left], 2);
            double powRight = Math.pow(nums[right], 2);
            if (powLeft > powRight) {
                res[currentIndex] = (int) powLeft;
                left++;
            } else {
                res[currentIndex] = (int) powRight;
                right--;
            }

            currentIndex--;
        }

        return res;
    }
}
